<template>
  <el-dropdown>
    <div>
      <svg-icon class-name="site-icon" icon-class="list" />
    </div>

    <el-dropdown-menu slot="dropdown">
      <el-dropdown-item v-for="item of sites" :key="item.code" @click.native="selSite(item.code)">
        {{
          item.title }}
      </el-dropdown-item>
    </el-dropdown-menu>
  </el-dropdown>
</template>

<script>
import { getAllSiteByUsername } from '@/api/site'
export default {
  data() {
    return {
      sites: null,
      site: null
    }
  },
  computed: {
    size() {
      return this.$store.getters.size
    }
  },
  mounted() {
    console.log('当前用户：' + sessionStorage.getItem('username'))
    this.getList(sessionStorage.getItem('username'))
  },
  methods: {
    getList(username) {
      getAllSiteByUsername(username).then(response => {
        this.sites = response.data
        console.log('get sites:' + this.sites)
        var i
        for (i = 0; i < this.sites.length; i++) {
          console.log('当前用户的对应站点：' + this.sites[i].title + this.sites[i].code)
        }
      })
    },
    selSite(code) {
      alert('切换站点:' + code)
    },
    selectSite(size) {
      // alert('this site1 :', this.site)

      this.$ELEMENT.size = size
      this.$store.dispatch('app/setSize', size)
      this.refreshView()
      this.$message({
        message: 'Switch Size Success',
        type: 'success'
      })
    },
    refreshView() {
      // In order to make the cached page re-rendered
      this.$store.dispatch('tagsView/delAllCachedViews', this.$route)

      const { fullPath } = this.$route

      this.$nextTick(() => {
        this.$router.replace({
          path: '/redirect' + fullPath
        })
      })
    }
  }

}
</script>
